import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/goods/goods.dart';
import 'package:xianyu_school/xianyuApp/home/List/homeGoodItem.dart';
import 'package:xianyu_school/xianyuApp/mine/Widgets/BallPulseIndicator.dart';

class followingPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _followingPageState();
  }
}

class _followingPageState extends State<followingPage>{

  String username;
  var url = urls().url;
  List<goods> gs = [];

  @override
  void initState() {
    super.initState();
  }

  Future getgoods() async {
    try{
      SharedPreferences pre = await SharedPreferences.getInstance();
      username = pre.getString("username");
      Response re = await Dio().get(url+"/users/getFollowingGoods",queryParameters: {"username":username});
      return re;
    }
    catch(e){
      return ;
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: Colors.yellow,
        middle: const Text("我的关注"),
      ),
      body: FutureBuilder(
          future: getgoods(),
          builder:(context,snapshot){
            if(snapshot.hasData){
              if(snapshot.data.toString()=="No more data"){
                Fluttertoast.showToast(
                    msg: "暂无数据",
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.BOTTOM,
                    timeInSecForIosWeb: 1,
                    backgroundColor: Colors.black45,
                    textColor: Colors.white,
                    fontSize: 16.0
                );
                return Text(" ");
              }
              else{
                var responseJson = json.decode(snapshot.data.toString());
                responseJson.forEach((data) {
                  gs.add(goods.fromJson(data));
                });
                return CustomScrollView(
                  slivers: [
                    // 商品列表
                    SliverPadding(
                      padding: EdgeInsets.fromLTRB(10, 5, 10, 10),
                      sliver: SliverStaggeredGrid.countBuilder(

                        crossAxisCount: 1,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10,
                        itemCount: gs.length,
                        itemBuilder: (BuildContext context, int index) {
                          return FHHomeGoodItem(model: gs[index],);
                        },
                        staggeredTileBuilder: (int index) {
                          return StaggeredTile.fit(2);
                        },
                      ),
                    ),

                    // 刷新控件
                  ],
                );
              }
            }
            else return Center(
              child: BallPulseIndicator(
                  color: Colors.black
              ),
            );
          }),
    );
  }
}